package com.qf.controller;

import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.service.AdminStatService;
import com.qf.util.ResponseUtil;
import com.qf.vo.DayStatis;
import com.qf.vo.DtsGoodsVo;
import com.qf.vo.DtsOrderVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/stat")
@CrossOrigin
@RestController
public class AdminStatController {

    @Autowired
    private AdminStatService adminStatService;

    //用户统计
    @RequestMapping("/user")
    @RequiresPermissions("admin:stat:user")
    @RequiresPermissionsDesc(menu = "统计",button = "用户统计")
    public Object statUser(){

        List<DayStatis> users = adminStatService.selectUser();

        HashMap[] rows = new HashMap[users.size()];

        for (int i = 0; i < users.size(); i++) {
            HashMap<String, Object> user = new HashMap<>();
            user.put("day", users.get(i).getDayStr());
            user.put("users", users.get(i).getCnts());
            rows[i] = user;
        }

        String[] columns = {"day","users"};

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows", rows);

        return ResponseUtil.ok(data);
    }

    //订单统计
    @RequestMapping("/order")
    @RequiresPermissions("admin:stat:order")
    @RequiresPermissionsDesc(menu = "统计",button = "订单统计")
    public Object statOrder(){

        List<DtsOrderVo> dtsOrderVos = adminStatService.selectOrder();

        String[] columns = {"day","orders","customers","amount", "pcr"};

        HashMap[] rows = new HashMap[dtsOrderVos.size()];
        for (int i = 0; i < dtsOrderVos.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("day", dtsOrderVos.get(i).getDay());
            map.put("orders", dtsOrderVos.get(i).getOrders());
            map.put("customers", dtsOrderVos.get(i).getCustomers());
            map.put("amount", dtsOrderVos.get(i).getAmount());
            map.put("pcr", dtsOrderVos.get(i).getPcr());

            rows[i] = map;
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows", rows);

        return ResponseUtil.ok(data);

    }


   //商品统计
    @RequestMapping("/goods")
    @RequiresPermissions("admin:stat:goods")
    @RequiresPermissionsDesc(menu = "统计",button = "商品统计")
    public Object statGoods(){
        List<DtsGoodsVo> dtsGoodsVos = adminStatService.selectGoods();

        String[] columns = {"day","orders","products", "amount"};

        HashMap[] rows = new HashMap[dtsGoodsVos.size()];
        for (int i = 0; i < dtsGoodsVos.size(); i++) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("day", dtsGoodsVos.get(i).getDay());
            map.put("orders", dtsGoodsVos.get(i).getOrders());
            map.put("products", dtsGoodsVos.get(i).getProducts());
            map.put("amount", dtsGoodsVos.get(i).getAmount());

            rows[i] = map;
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("columns", columns);
        data.put("rows", rows);

        return ResponseUtil.ok(data);
    }
}
